using UnityEngine;
using System.Collections;

public class MenuManager : MonoBehaviour 
{
	public static MenuManager Instance;
	
	public Texture[] bgImage;
	public GUITexture bgTexture;
	
	public enum Screens
	{
		MAIN_MENU,
		SETTINGS,
		DEFAULT
	}
	
	public bool ENABLE_AUDIO		= true;
	public bool ENABLE_SOUND		= true;
	
	public UICheckbox audioCbox		= default(UICheckbox);
	public UICheckbox soundCbox		= default(UICheckbox);
	
	public Screens currentScreen 	= Screens.DEFAULT;
	public GameObject[] screenPanels;
	

	void Awake ()
	{
		Instance = this;
	}
	
	void Start () 
	{
		bgTexture.texture = bgImage[0];
		bgTexture.pixelInset = new Rect( -Screen.width/2, -Screen.height/2, Screen.width, Screen.height );
		CheckCBox ();
		screenPanels[1].SetActive(false);
	}

	void FixedUpdate () 
	{
		if ( currentScreen == Screens.SETTINGS )
			bgTexture.texture = bgImage[1];
		else
			bgTexture.texture = bgImage[0];
//		bgTexture.texture = bgImage;
//		bgTexture.pixelInset = new Rect( -Screen.width/2, -Screen.height/2, Screen.width, Screen.height );
	}
	
	void GoToSettingsScreen ()
	{
		currentScreen = Screens.SETTINGS;
		
		screenPanels[1].SetActive(true);
		
		screenPanels[0].SetActive(false);
		
		
	}
	
	void GoToMainMenuScreen ()
	{
		currentScreen = Screens.MAIN_MENU;
		screenPanels[0].SetActive(true);
		screenPanels[1].SetActive(false);
	}
	
	
	#region MAIN MENU BUTTONS
	void GoToCampaign ()
	{
		Application.LoadLevel( "GenericLevel" );
	}
	
	void GoToSingleRace ()
	{
		Application.LoadLevel( "GenericLevel" );
	}
	
	void GoToSettings ()
	{
		GoToSettingsScreen ();
	}
	
	void GoToFacebook ()
	{
	}
	
	void GoToTwitter ()
	{
	}
	
	void GoToGameCenter ()
	{
	}
	#endregion
	
	#region SETTINGS BUTTONS
	void GoToMainMenu ()
	{
		GoToMainMenuScreen ();
	}
	
	void GoToCredits ()
	{
	}
	
	void ToggleAudio ( bool isActive )
	{
		if ( isActive )
		{
			ENABLE_AUDIO = true;
			PlayerPrefs.SetInt("Audio" , 1 );
		}
		else
		{
			ENABLE_AUDIO = false;
			PlayerPrefs.SetInt("Audio" , 0 );
		}
		
		CheckSounds ();
	}
	
	void ToggleSounds ( bool isActive )
	{
		if ( isActive )
		{
			ENABLE_SOUND = true;
			PlayerPrefs.SetInt("Sound" , 1 );
		}
		else
		{
			ENABLE_SOUND = false;
			PlayerPrefs.SetInt("Sound" , 0 );
		}
		
		CheckSounds ();
	}
	
	void CheckSounds ()
	{
		if ( PlayerPrefs.GetInt("Audio") == 0 && PlayerPrefs.GetInt("Sound") == 0 )
		{
			AnimatedGUI.Instance.AnimateMeTrigger( false );
		}
		else if ( PlayerPrefs.GetInt("Audio") == 1 || PlayerPrefs.GetInt("Sound") == 1 )
		{
			AnimatedGUI.Instance.AnimateMeTrigger( true );
		}
	}
	
	void CheckCBox ()
	{
		CheckSounds ();
		
		if ( PlayerPrefs.GetInt("Audio") == 1 )
			audioCbox.isChecked = true;
		else
			audioCbox.isChecked = false;
		
		if ( PlayerPrefs.GetInt("Sound") == 1 )
			soundCbox.isChecked = true;
		else
			soundCbox.isChecked = false;
		
		
	}
	#endregion
	
}
